import axios from "axios";
import { getToken } from "./auth";

const instance = axios.create({

  baseURL: "/api",
  // withCredentials: true
});

instance.interceptors.request.use(
  function (config) {
    // 判断有没有token
    if (getToken()) {
      config.headers.authorization = "Bearer " + getToken();
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  function (response) {
    return response;
  },
  function (error) {
    return Promise.reject(error);
  }
);

// 封装get请求
export const get = (url, params) => instance.get(url, { params });

// 封装post请求
export const post = (url, data) => instance.post(url, data);
